#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Lee
# @File    : dhy_threshold_dialog.py
# @Time    : 2024/1/12 17:19
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
from views.resource_view.threshold_dialog import Ui_threshold_dialog


class DHYThresholdDialog(QtWidgets.QDialog, Ui_threshold_dialog):

    # 自定义信号
    threshold_value_changed_signal = pyqtSignal(dict)

    def __init__(self, global_config, parent=None):
        super(DHYThresholdDialog, self).__init__(parent)
        self.setupUi(self)
        self.global_config = global_config

        self.reset_btn.clicked.connect(self.reset_value)
        self.cancel_btn.clicked.connect(self.close_dialog)
        self.set_btn.clicked.connect(self.set_threshold_value)
        self.reset_value()

    def reset_value(self):
        self.spin_box_aver_count.setValue(self.global_config["aver_count"])
        self.spin_box_online_dis.setValue(self.global_config["online_dis"])
        self.spin_box_start_dis.setValue(self.global_config["start_to_a_dis"])
        self.spin_box_end_dis.setValue(self.global_config["end_to_a_dis"])

    def set_threshold_value(self):
        _value = {
            "aver_count": int(self.spin_box_aver_count.value()),
            "online_dis": float(self.spin_box_online_dis.value()),
            "start_to_a_dis": float(self.spin_box_start_dis.value()),
            "end_to_a_dis": float(self.spin_box_end_dis.value()),
        }
        self.threshold_value_changed_signal.emit(_value)

    def close_dialog(self):
        self.close()
